<?php
/*
* @Descripttion: 插件
* @Author: jiosen <4631458@qq.com>
* @Date: 2024-06-18 17:17:38
*/
namespace app\controller;

use app\BaseController;
use plugins\Base;

class Plugin extends BaseController
{
    // 插件列表
    public function lists()
    {
        $base = new Base();
        $plugins = $base->getPlugins();
        if($plugins){
            $installPlugins = $base->getInstallPlugins();
            $installPlugins = array_column($installPlugins,null,'name');
            foreach($plugins as $key=>$value){
                if(!empty($installPlugins[$value['class']])){
                    $plugins[$key]['install'] = 1;
                    $plugins[$key]['config'] = $installPlugins[$value['class']];
                    
                }else{
                    $plugins[$key]['install'] = 0;
                    $plugins[$key]['config'] = [];
                }
            }
        }
        

        return $this->success('success',$plugins);

    }

    // 安装插件
    public function install()
    {
        $name = $this->request->param('name','','trim');
        $res = (new Base())->install($name);

        if($res['state'] == 0){
            return $this->error($res['msg']);
        }

        return $this->success('安装成功');
    }

    // 卸载插件
    public function uninstall()
    {
        $name = $this->request->param('name','','trim');
        $res = (new Base())->uninstall($name);

        if($res['state'] == 0){
            return $this->error($res['msg']);
        }

        return $this->success('卸载成功');
    }

    // 设置
    public function setting()
    {
        $name = $this->request->param('name','','trim');
        $res = (new Base())->getSetting($name);

        if($res['state'] == 0){
            return $this->error($res['msg']);
        }

        return $this->success('获取成功',$res['data']);
    }

    // 保存设置
    public function setSetting()
    {
        $data = $this->request->param();

        $res = (new Base())->setSetting($data);

        if($res['state'] == 0){
            return $this->error($res['msg']);
        }

        return $this->success('保存成功');
    }

 }